package cn.rkylin.oms.order.controller;

import java.net.URLDecoder;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.bson.util.StringRangeSet;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.rkylin.core.controller.ApolloController;
import cn.rkylin.oms.common.dto.DBLog;
import cn.rkylin.oms.common.utils.LogUtil;
import cn.rkylin.oms.order.adapter.OrderAdapter;
import cn.rkylin.oms.order.adapter.OrderAdapterFactory;
import cn.rkylin.oms.refund.controller.RefundController;

/**
 * 自动下载指定店铺的订单
 * 
 */
@Controller
@RequestMapping("/orderManager")
public class OrderController extends ApolloController {
	private static final Log logger = LogFactory.getLog(RefundController.class);
	private static final SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

	@Autowired
	LogUtil logUtil;

	/**
	 * 平台发货 http://localhost:8080/OMSJobExecutor/orderManager/consign.action
	 * 
	 * @param shopId
	 *            指定的店铺
	 * @param start
	 *            开始时间
	 * @param end
	 *            结束时间
	 * @return 结果
	 * @throws Exception
	 */
	@ResponseBody
	@RequestMapping(value = "/consign", method = RequestMethod.GET)
	public Map consign(String shopId, String start, String end) {
		
		// 测试
		// shopId = "008480ccb1d64b30b843023fe5b13d3a";
		
		// 返回值
		Map<String, Object> resultMap = new HashMap<String, Object>();
		try {
		
			if (StringUtils.isBlank(shopId)) {
				resultMap.put("success", "false");
				resultMap.put("msg", "shopId is null");
				return resultMap;
			}

			// 时间相关校验
			if (StringUtils.isEmpty(start) && StringUtils.isNotEmpty(end)) {
				Calendar c = Calendar.getInstance();
				c.setTime(simpleDateFormat.parse(end));
				c.add(Calendar.MONTH, -3); // 得到前3个月
				start = simpleDateFormat.format(c.getTime());
				end = simpleDateFormat.format(new Date());
			} else {
				if (StringUtils.isNotBlank(start)) {
					start = URLDecoder.decode(start, "UTF-8");
				}
				if (StringUtils.isNotBlank(end)) {
					end = URLDecoder.decode(end, "UTF-8");
				}
			}

			OrderAdapter orderAdapter = OrderAdapterFactory.getOrderAdapter(shopId);
			if (orderAdapter == null) {
				resultMap.put("result", "adapter is null");
				resultMap.put("success", "false");
				return resultMap;
			}

			long startTime1 = System.currentTimeMillis();
			resultMap = orderAdapter.consign(shopId, start, end);
			logger.error(">>>>>>>>>>>> 平台发货总用时:" + (System.currentTimeMillis() - startTime1) + "<<<<<<<<<<<");
		} catch (Exception e) {
			resultMap.put("success", "false");
			resultMap.put("msg", e.getMessage());
			logUtil.error(new DBLog("", "job_exception", "consign", "自动任务  平台发货异常: " + e.getMessage()), e);
		}
		return resultMap;
	}

	/**
	 * 根据条件下载平台订单
	 * http://localhost:8085/orderManager/downLoadOrder.action?shopId=ad4d1392e16511e7910200163e035834&ecTradeCode=88641452106925051
	 * http://localhost:8085/orderManager/downLoadOrder.action?shopId=ad4d1392e16511e7910200163e035834&buyerNick=雅痞。。。1573578516
	 * @param shopId
	 * @param ecTradeCode
	 * @param buyerNick
	 * @return
	 * @throws Exception
	 */
	@ResponseBody
	@RequestMapping(value = "/downLoadOrder", method = RequestMethod.GET)
	public Map downLoadOrder(String shopId, String ecTradeCode, String buyerNick) throws Exception {
		// 返回值
		//buyerNick="酷爱小kk";
		Map<String, Object> resultMap = new HashMap<String, Object>();

		OrderAdapter taobaoOrderAdapter = OrderAdapterFactory.getOrderAdapter(shopId);
		if (taobaoOrderAdapter == null) {
			resultMap.put("result", "adapter is null");
			resultMap.put("success", "false");
			return resultMap;
		}

		// 订单下载 分单
		int result = 0;
		if (StringUtils.isNotBlank(shopId)) {
			Map<String, String> map = new HashMap<String, String>();
			map.put("shopId", shopId);
			map.put("ecTradeCode", ecTradeCode);
			map.put("buyerNick", buyerNick);
			result = taobaoOrderAdapter.download(map);
		}

		if (result == 0) {
			resultMap.put("success", "false");
		} else {
			resultMap.put("success", "true");
		}

		return resultMap;
	}

	/**
	 * 自动下载指定店铺的订单
	 * http://localhost:8085/orderManager/getOrderAll.action?shopId=ad4d1392e16511e7910200163e035834&start=2017-11-1&end=2017-12-12
	 * 
	 * @param shopId
	 *            指定的店铺
	 * @param start
	 *            开始时间
	 * @param end
	 *            结束时间
	 * @return
	 * @throws Exception
	 */
	@ResponseBody
	@RequestMapping(value = "/getOrderAll", method = RequestMethod.GET)
	public Map getOrderAll(String shopId, String start, String end) throws Exception {

		if (StringUtils.isEmpty(start) && StringUtils.isNotEmpty(end)) {
			Calendar c = Calendar.getInstance();
			c.setTime(simpleDateFormat.parse(end));
			c.add(Calendar.MONTH, -3); // 得到前3个月
			start = simpleDateFormat.format(c.getTime());
		}
		start = URLDecoder.decode(start, "UTF-8");
		end = URLDecoder.decode(end, "UTF-8");

		// 返回值
		Map<String, Object> resultMap = new HashMap<String, Object>();

		OrderAdapter taobaoOrderAdapter = OrderAdapterFactory.getOrderAdapter(shopId);
		if (taobaoOrderAdapter == null) {
			resultMap.put("result", "adapter is null");
			resultMap.put("success", "false");
			return resultMap;
		}

		// 订单下载 分单
		int result = 0;
		if (StringUtils.isNotBlank(shopId)) {
			long startTime1 = System.currentTimeMillis();
			result = taobaoOrderAdapter.download(shopId, start, end);
			logger.error(">>>>>>>>>>>> 下载订单总用时:" + (System.currentTimeMillis() - startTime1) + "<<<<<<<<<<<");
		}

		if (result == 0) {
			resultMap.put("success", "false");
		} else {
			resultMap.put("success", "true");
		}

		return resultMap;
	}

}
